<script>
	import {
		getStorage,
		removeStorage
	} from '@/util/auth.js';
	import {
		isLogin
	} from '@/util/user.js'
	import {
		initIM
	} from '@/util/im.js'
	export default {
		onLaunch: function() {
			//监听路由跳转
			uni.loadFontFace({
				source: 'url("https://guoyuan-1306862033.cos.ap-chengdu.myqcloud.com/%E6%96%B9%E6%AD%A3%E6%B1%89%E7%9C%9F%E5%B9%BF%E6%A0%87%E7%AE%80%E4%BD%93.TTF")',
				family: 'FangZhengHanZhenGuangBiaoJianTi',
				global: true,
			})
			let _this = this;
			uni.addInterceptor('navigateTo', {
				invoke(e) {
					return _this.watchRouter(e);
				}
			})
			uni.addInterceptor('redirectTo', {
				invoke(e) {
					return _this.watchRouter(e);
				}
			})
			uni.addInterceptor('reLaunch', {
				invoke(e) {
					return _this.watchRouter(e);
				}
			})
			uni.addInterceptor('switchTab', {
				invoke(e) {
					return _this.watchRouter(e);
				}
			})
			initIM().then(() => {
				const hasToken = this.$store.getters.token;
				if (hasToken) {
					this.$store.dispatch('user/loginIM')
				}
			}).catch((err) => {
				this.showToast(err)
			})

			uni.$on('onSdkReady', () => {
				this.$store.commit('user/SET_READY', true)
			})
		},
		methods: {
			watchRouter(e) {
				let url = e.url;
				const index = url.indexOf('?');
				url = index == -1 ? url : url.slice(0, index);
				const hasToken = getStorage('token');
				const user = getStorage('user');
				const identity = getStorage('identity');
				const whiteList = ['/pages/home/index', '/pages/mine/index', '/pages/index/index', '/pages/index/entrance',
					'/page_other/login/index', '/page_other/login/select', '/page_other/job_hunting/job_hunting',
					'/page_other/job_category/index',
					'/page_other/sweep_matching/matching', '/page_other/sweep_matching/form',
					'/page_other/job_hunting/job_detail/index', '/page_other/company/company',
					'/page_other/job_hunting/job_fair/index', '/page_other/job_hunting/job_fair/detail',
					'/page_other/job_hunting/job_fair/list', '/page_other/job_hunting/job_fair/job_company',
					'/page_other/job_hunting/job_list/index', '/page_other/article/detail',
					'/page_other/base_map/index',
					'/page_other/article/index', '/page_other/policy/index', '/page_other/difficult_job/index',
					'/page_other/difficult_job/detail', '/page_other/train/index', '/page_other/train/detail',
					'/page_other/train/train_list', '/page_other/train/course',
					'/page_other/train/police', '/page_other/policy/detail',
					'/page_other/obtain_employment/obtain_employment'

				];
				if (['/pages/home/index', '/pages/index/index', '/pages/index/entrance', ].includes(url)) {
					return true;
				}
				if (hasToken) {
					if (user) {
						if (['/page_other/resume/personal_information', '/page_other/enterprise/authorization/person-info',
								'/page_other/enterprise/authorization/authentication',
								'/page_other/enterprise/authorization/business',
								'/page_other/enterprise/authorization/examine'
							].includes(url)) {
							return true;
						}
						if (identity == 1) {
							if (user.infoStatus == 0) { //如果没有完善个人信息就强制跳转到个人中心
								this.showToast('请先完善个人信息', () => {
									uni.navigateTo({
										url: '/page_other/resume/personal_information'
									})
								})
								return false;
							}
							return true
						} else {
							let url = '';
							switch (user.authenticationStatus) {
								case 0:
									url = '/page_other/enterprise/authorization/person-info?type=perfect';
									break;
								case 1:
									url = '/page_other/enterprise/authorization/authentication';
									break;
								case 2:
									url = '/page_other/enterprise/authorization/business';
									break;
							}
							if (url) {
								const message = '请完善信息'
								this.showToast(message, () => {
									uni.navigateTo({
										url
									})
								})
								return false;
							}
							return true;
						}
					} else {
						this.$store.commit('user/SET_TOKEN', null)
						this.$store.commit('user/SET_USER', null)
						this.$store.commit('user/SET_IDENTITY', null)
						this.showToast('出错，请重新登录', () => {
							uni.redirectTo({
								url: '/page_other/login/select'
							})
						})
					}
				} else {
					if (whiteList.includes(url)) {
						return true
					} else {
						uni.showModal({
							title: '提示',
							content: '需要先登录！',
							cancelText: '先逛逛',
							confirmText: '去登录',
							success(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/page_other/login/select'
									})
								} else {
									// uni.navigateBack();
								}
							}
						})
					}
				}
				return false;
			}
		},
	}
</script>
<style lang="scss">
	@import url("@/static/icon/iconfont.css");

	view,
	text,
	navigator,
	input,
	picker,
	button,
	image {
		box-sizing: border-box;
	}

	.FangZhengHanZhenGuangBiaoJianTi {
		font-family: 'FangZhengHanZhenGuangBiaoJianTi';
	}

	.empty {
		text-align: center;
		color: #313131;
		font-size: 28rpx;
		margin: 30rpx 0;
	}

	page {
		min-height: calc(100vh - var(--window-bottom) - var(--window-top));
		background: rgb(248, 248, 248);
	}

	.content {
		width: 100%;
	}

	.inputPlaceholder {
		color: #999;
		font-weight: normal;
	}

	.fw {
		font-weight: bold;
	}

	.fs17 {
		font-size: 34rpx;
	}

	.fs15 {
		font-size: 30rpx;
	}

	.fs12 {
		font-size: 24rpx;
	}

	.fs13 {
		font-size: 26rpx;
	}

	.fs14 {
		font-size: 28rpx;
	}

	.darkgray {
		color: #999
	}

	.mt20 {
		margin-top: 20rpx;
	}

	.mr20 {
		margin-right: 20rpx;
	}

	.mb20 {
		margin-bottom: 20rpx;
	}

	.ml20 {
		margin-left: 20rpx;
	}

	.mt30 {
		margin-top: 30rpx;
	}

	.mr30 {
		margin-right: 30rpx;
	}

	.mb30 {
		margin-bottom: 30rpx;
	}

	.ml30 {
		margin-left: 30rpx;
	}

	.overflow-line {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	/* radio 选中后的样式 */
	uni-radio .uni-radio-input.uni-radio-input-checked {
		background-color: #ff7626 !important;
		border-color: #ff7626 !important;
		background-clip: content-box !important;
		padding: 6rpx !important;
		box-sizing: border-box;
	}

	/* radio 选中后的图标样式*/
	uni-radio .uni-radio-input.uni-radio-input-checked::before {
		display: none !important;
	}
</style>